package com.wgts.wgts.mvp.view.activity.shopkeeper;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.LazyBaseFragment;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerMyExtensionFansOrderFragmentComponent;
import com.wgts.wgts.dagger.module.MyExtensionFansOrderFragmentModule;
import com.wgts.wgts.mvp.presenter.IMyExtensionFansOrderFragmentPresenter;
import com.wgts.wgts.mvp.view.IMyExtensionFansOrderFragmentView;
import com.wgts.wgts.netmodle.resp.OperatingDataEntity;
import com.wgts.wgts.netmodle.resp.ShopKeepFansOrderSum;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MyExtensionFansOrderFragment extends LazyBaseFragment implements IMyExtensionFansOrderFragmentView {
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.tv_all_util)
    TextView tvAllUtil;
    @BindView(R.id.tv_shop_keeper)
    TextView tvShopKeeper;
    @BindView(R.id.tv_shop_keeper_util)
    TextView tvShopKeeperUtil;
    @BindView(R.id.tv_nomal_user)
    TextView tvNomalUser;
    @BindView(R.id.tv_nomal_user_util)
    TextView tvNomalUserUtil;
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    Unbinder unbinder;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_keeper)
    TextView tvKeeper;
    @BindView(R.id.tv_money)
    TextView tvMoney;

    private MyExtensionFansOrderFragmentToActivity myExtensionFansOrderFragmentToActivity;

    private List<OperatingDataEntity.Entity> srcData;

    public static final String USER_ID = "USER_ID";

    private OperatingDataAdapter adapter;

    private static final Double THOUSAND = 10000d;

    private boolean noMore;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        myExtensionFansOrderFragmentToActivity = (MyExtensionFansOrderFragmentToActivity) context;
    }


    @Inject
    IMyExtensionFansOrderFragmentPresenter presenter;

    @Override
    protected void loadData() {
        adapter = new OperatingDataAdapter(getContext(), srcData);
        recycleView.setAdapter(adapter);
        presenter.initShopKeepFansOrderList(getArguments().getString(USER_ID, ""));
        presenter.shopKeepFansOrderSum(getArguments().getString(USER_ID, ""));
    }

    @Override
    public void init() {
        srcData = new ArrayList<>();
        DaggerMyExtensionFansOrderFragmentComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .myExtensionFansOrderFragmentModule(new MyExtensionFansOrderFragmentModule(this))
                .build()
                .inject(this);


    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.my_extension_fans_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recycleView.setLayoutManager(linearLayoutManager);
        tvNumber.setText("订单数");
        tvKeeper.setText("成交额");
        tvMoney.setText("佣金");

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void moreOrderList(OperatingDataEntity entity, boolean noMore) {

        myExtensionFansOrderFragmentToActivity.loadFinish();

        this.noMore = noMore;
        adapter.setNoMore(noMore);
        srcData.addAll(entity.list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void initOrderList(OperatingDataEntity entity, boolean noMore) {
        this.noMore = noMore;
        srcData.clear();
        adapter.setNoMore(noMore);
        srcData.addAll(entity.list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void shopKeepFansOrderSum(ShopKeepFansOrderSum shopKeepFansOrderSum) {
        DecimalFormat df = new DecimalFormat("#.##");

        if (shopKeepFansOrderSum.orderCount / THOUSAND < 1) {
            tvAllUtil.setVisibility(View.INVISIBLE);
            tvAll.setText(String.valueOf(shopKeepFansOrderSum.orderCount));
        } else {
            tvAllUtil.setVisibility(View.VISIBLE);
            tvAll.setText(df.format(shopKeepFansOrderSum.orderCount / THOUSAND));
        }


        if (Double.valueOf(shopKeepFansOrderSum.summaryTurnover) / THOUSAND < 1) {
            tvShopKeeperUtil.setVisibility(View.INVISIBLE);
            tvShopKeeper.setText(shopKeepFansOrderSum.summaryTurnover);
        } else {
            tvShopKeeperUtil.setVisibility(View.VISIBLE);
            tvShopKeeper.setText(df.format(Double.valueOf(shopKeepFansOrderSum.summaryTurnover) / THOUSAND));
        }


        if (Double.valueOf(shopKeepFansOrderSum.summaryMoney) / THOUSAND < 1) {
            tvNomalUserUtil.setVisibility(View.INVISIBLE);
            tvNomalUser.setText(shopKeepFansOrderSum.summaryMoney);
        } else {
            tvNomalUserUtil.setVisibility(View.VISIBLE);
            tvNomalUser.setText(df.format(Double.valueOf(shopKeepFansOrderSum.summaryMoney) / THOUSAND));
        }
    }

    @Override
    public void onError(int status, String msg) {

    }

    public static Fragment getInstance(String userId) {
        Fragment fragment = new MyExtensionFansOrderFragment();
        Bundle bundle = new Bundle();
        bundle.putString(USER_ID, userId);
        fragment.setArguments(bundle);
        return fragment;
    }

    public void loadMore() {
        if (!noMore && presenter != null) {
            presenter.moreShopKeepFansOrderList();
        } else {
            myExtensionFansOrderFragmentToActivity.loadFinish();
        }
    }

    public interface MyExtensionFansOrderFragmentToActivity {
        void loadFinish();
    }


}
